package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.LeaseRealHandleDeviceInfo;
import com.isunland.managesystem.entity.LeaseRealHandleDeviceInfoDetailOriginal;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 */

public class LeaseActualDetailFragment extends Fragment {
  public static final String EXTRA_ITEM =
      LeaseActualDetailFragment.class.getSimpleName() + ".EXTRA_ITEM";
  @BindView(R.id.tv_equipmentTypeColon) EditText tvEquipmentTypeColon;
  @BindView(R.id.tv_deviceNo) EditText tvDeviceNo;
  @BindView(R.id.tv_brandType) EditText tvBrandType;
  @BindView(R.id.tv_manufacturer) TextView tvManufacturer;
  @BindView(R.id.tv_manufactureDate) TextView tvManufactureDate;
  @BindView(R.id.tv_handleStaffName) EditText tvHandleStaffName;
  @BindView(R.id.tv_handleDate) EditText tvHandleDate;
  @BindView(R.id.tv_validTil) EditText tvValidTil;
  @BindView(R.id.tv_runStatusText) EditText tvRunStatusText;
  @BindView(R.id.tv_docPic) EditText tvDocPic;
  @BindView(R.id.tv_register) EditText tvRegister;
  @BindView(R.id.tv_registerTime) EditText tvRegisterTime;
  @BindView(R.id.tv_text) EditText tvText;

  private String mId;
  private LeaseRealHandleDeviceInfo mItem;
  private BaseVolleyActivity mActivity;

  public static LeaseActualDetailFragment newInstance(LeaseRealHandleDeviceInfo item) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_ITEM, item);

    LeaseActualDetailFragment fragment = new LeaseActualDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  //初始化数据
  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    mActivity = ((BaseVolleyActivity) getActivity());
    mItem = (LeaseRealHandleDeviceInfo) getArguments().getSerializable(EXTRA_ITEM);

    if (mItem != null) {
      mId = mItem.getId();
    }
  }

  //初始化界面
  @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    ActionBar actionBar = mActivity.getSupportActionBar();
    if (actionBar != null) {
      actionBar.setTitle(R.string.actualHandleDetail);
      actionBar.setDisplayHomeAsUpEnabled(true);
    }
    View v = inflater.inflate(R.layout.fragment_lease_actual_detail, container, false);
    ButterKnife.bind(this, v);
    return v;
  }

  //联网请求.
  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    bindData(mItem);
    volleyPostInit(mId);
  }

  protected void volleyPostInit(String id) {
    if (TextUtils.isEmpty(id)) return;
    String mId = mItem.getId();

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_LEASE_ACTUAL_DETAIL);
    HashMap<String, String> params = new HashMap<>();
    params.put("id", mId);

    MyUtils.showDialogLoading(mActivity);
    mActivity.volleyPost(url, params, new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {
        ToastUtil.show("获取详情失败!");
      }

      @Override public void onVolleyResponse(String json) throws JSONException {
        //json = MyUtils.readAssetsFile(MyApplication.getAppContext(), "testOperationDetail.json");
        LeaseRealHandleDeviceInfoDetailOriginal original =
            new Gson().fromJson(json, LeaseRealHandleDeviceInfoDetailOriginal.class);
        if (original.getResult() == Base.SUCCESS && original.getData() != null) {

          bindData(original.getData());
        } else {
          ToastUtil.show("获取详情失败!");
        }
      }
    });
  }

  private void bindData(LeaseRealHandleDeviceInfo data) {
    if (data == null) return;
    tvEquipmentTypeColon.setText(data.getDeviceType());
    tvRegister.setText(data.getRegStaffName());
    tvRegisterTime.setText(data.getRegDate());
    tvDeviceNo.setText(data.getDeviceNo());
    tvBrandType.setText(data.getBrandType());
    tvManufacturer.setText(data.getManufacturer());
    tvManufactureDate.setText(data.getManufactureDate());
    tvHandleStaffName.setText(data.getHandleStaffName());
    tvHandleDate.setText(data.getHandleDate());
    tvValidTil.setText(data.getValidTil());
    tvDocPic.setText(data.getDocPic());
    tvRunStatusText.setText(data.getRunStatusText());
    tvText.setText(data.getText());
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        mActivity.finish();
        break;

      default:
        break;
    }

    return super.onOptionsItemSelected(item);
  }
}
